﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Zhp.Common.Extention;

namespace Zhp.Common.Helper
{
    public static class HttpHelper
    {
        public static string GetControllerName(this HttpContext httpContext)
        {
            return httpContext.GetRouteValue("controller")?.ToString();
        }

        public static string GetActionName(this HttpContext httpContext)
        {
            return httpContext.GetRouteValue("action")?.ToString();
        }

        public static string GetIP(this HttpContext httpContext)
        {
            return httpContext.Connection.RemoteIpAddress?.ToString() ?? "0.0.0.0";
        }

        /// <summary>
        /// 获取所有请求的参数（包括get参数和post参数）
        /// </summary>
        /// <param name="context">请求上下文</param>
        /// <returns></returns>
        public static async Task<Dictionary<string, object>> GetAllRequestParams(this HttpContext context)
        {
            Dictionary<string, object> allParams = new Dictionary<string, object>();

            var request = context.Request;
            List<string> paramKeys = new List<string>();
            var getParams = request.Query.Keys.ToList();
            var postParams = new List<string>();
            try
            {
                if (request.Method.ToLower() != "get")
                    postParams = request.Form.Keys.ToList();
            }
            catch
            {
                // ignored
            }

            paramKeys.AddRange(getParams);
            paramKeys.AddRange(postParams);

            paramKeys.ForEach(p =>
            {
                object value = null;
                if (request.Query.ContainsKey(p))
                {
                    value = request.Query[p].ToString();
                }
                else if (request.Form.ContainsKey(p))
                {
                    value = request.Form[p].ToString();
                }

                if (p.ToLower() == "password")
                {
                    allParams.Add(p, "********");
                }
                else
                {
                    allParams.Add(p, value);
                }
            });

            string contentType = request.ContentType?.ToLower() ?? "";

            //若为POST的application/json
            if (contentType.Contains("application/json"))
            {
                var stream = request.Body;
                string str = await stream.ReadToString(Encoding.UTF8);

                if (!string.IsNullOrEmpty(str))
                {
                    var obj = str.ToJObject();
                    foreach (var aProperty in obj)
                    {
                        if (aProperty.Key.ToLower() == "password")
                        {
                            allParams[aProperty.Key] = "********";
                        }
                        else
                        {
                            allParams[aProperty.Key] = aProperty.Value;
                        }
                    }
                }
            }

            return allParams;
        }
    }
}
